package gold.digger;

import gold.vo.TreeNode;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC513 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int findBottomLeftValue(TreeNode root) {
            Deque<TreeNode> curLayer = new LinkedList<>();
            Deque<TreeNode> nextLayer = new LinkedList<>();
            curLayer.addLast(root);

            TreeNode cur, layerFirst;
            while (!curLayer.isEmpty()) {
                layerFirst = curLayer.getFirst();
                while (!curLayer.isEmpty()) {
                    cur = curLayer.pollFirst();
                    if (cur.left != null) nextLayer.addLast(cur.left);
                    if (cur.right != null) nextLayer.addLast(cur.right);
                }

                if (nextLayer.isEmpty()) return layerFirst.val;
                Deque temp = curLayer;
                curLayer = nextLayer;
                nextLayer = temp;
            }

            return 0;
        }
    }

    public void run() {
        Solution solution = new Solution();
        TreeNode tr = TreeNode.createTree("1,2,3,4,null,5,6,null,null,7");
        System.out.println(solution.findBottomLeftValue(tr));
    }

    public static void main(String[] args) throws Exception {
        LC513 an = new LC513();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
